<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="search-form">
      <el-form-item label="项目名称" prop="exName">
        <el-input
          v-model="queryParams.exName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8 operation-row">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:examintions:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:examintions:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:examintions:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:examintions:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="examintionsList"
      @selection-change="handleSelectionChange"
      class="custom-table"
      :header-cell-style="{
        background: '#f5f7fa',
        color: '#606266',
        fontWeight: 'bold'
      }"
      :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目id" align="center" prop="exId" />
      <el-table-column label="项目名称" align="center" prop="exName" />
      <el-table-column label="项目价格" align="center" prop="exPrice" />
      <el-table-column label="所属套餐" align="center" prop="poName" />
      <el-table-column label="项目介绍" align="center" prop="introduction" />
      <el-table-column label="注意事项" align="center" prop="precautions" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
            <span v-if="scope.row.status === 1">
              <el-button type="success" icon="el-icon-check" circle></el-button>
            </span>
          <span v-else>
              <el-button type="danger" icon="el-icon-close" circle></el-button>
            </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:examintions:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:examintions:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目体检对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
      custom-class="custom-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="custom-form">
        <el-form-item label="项目名称" prop="exName">
          <el-input v-model="form.exName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="所属套餐" prop="exName">
          <el-select v-model="form.poType" placeholder="请选择">
            <el-option
              v-for="item in option"
              :key="item.id"
              :label="item.packageName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目价格" prop="exPrice">
          <el-input v-model="form.exPrice" placeholder="请输入项目价格" />
        </el-form-item>
        <el-form-item label="项目介绍" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入项目介绍" />
        </el-form-item>
        <el-form-item label="注意事项" prop="precautions">
          <el-input v-model="form.precautions" placeholder="请输入注意事项" />
        </el-form-item>
        <el-form-item label="项目状态" prop="precautions">
          <el-input v-model="form.status" placeholder="请输入项目状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" class="submit-btn">确 定</el-button>
        <el-button @click="cancel" class="cancel-btn">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExamintions, getExamintions, delExamintions, addExamintions, updateExamintions } from "@/api/system/examintions"
import {getExamPackageName, listPackage} from "@/api/system/package";

export default {
  name: "Examintions",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目体检表格数据
      examintionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      option: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        exName: null,
        exPrice: null,
        poType: null,
        introduction: null,
        precautions: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询项目体检列表 */
    getList() {
      this.loading = true
      listExamintions(this.queryParams).then(response => {
        this.examintionsList = response.rows
        this.total = response.total
        this.loading = false
      })
      getExamPackageName().then(res=>{
        this.option = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        exId: null,
        exName: null,
        exPrice: null,
        poType: null,
        introduction: null,
        precautions: null,
        status: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.exId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加项目体检"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const exId = row.exId || this.ids
      getExamintions(exId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改项目体检"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.exId != null) {
            updateExamintions(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addExamintions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const exIds = row.exId || this.ids
      this.$modal.confirm('是否确认删除项目体检编号为"' + exIds + '"的数据项？').then(function() {
        return delExamintions(exIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/examintions/export', {
        ...this.queryParams
      }, `examintions_${new Date().getTime()}.xlsx`)
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2 === 0) {
        return 'even-row';
      }
      return 'odd-row';
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);

  .search-form {
    margin-bottom: 20px;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 4px;
  }

  .operation-row {
    margin-bottom: 20px;

    .el-button {
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
      }
    }
  }

  .custom-table {
    border-radius: 8px;
    overflow: hidden;

    .even-row {
      background-color: #fafafa;
    }

    .odd-row {
      background-color: #ffffff;
    }

    .el-button--text {
      padding: 4px 8px;
      transition: all 0.3s;

      &:hover {
        color: #409EFF;
        background: rgba(64,158,255,0.1);
        border-radius: 4px;
      }
    }
  }
}

.custom-dialog {
  .custom-form {
    padding: 20px;

    .el-form-item {
      margin-bottom: 22px;
    }

    .el-input, .el-select {
      width: 100%;
    }
  }

  .dialog-footer {
    padding: 20px;
    text-align: right;

    .submit-btn {
      background: #409EFF;
      border-color: #409EFF;
      padding: 10px 20px;

      &:hover {
        background: #66b1ff;
        border-color: #66b1ff;
      }
    }

    .cancel-btn {
      padding: 10px 20px;
    }
  }
}

// 添加动画效果
.el-table__row {
  transition: all 0.3s;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  }
}

// 分页样式优化
.pagination-container {
  padding: 20px 0;
  text-align: right;
}
</style>
